package com.example.teemor.es_demo.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = false)
@ToString
@Document(indexName = "product")
public class ProductES {
    @Id
    @Field(type = FieldType.Long)
    private Long id;

    private String title;
    private String description;
    private BigDecimal price;
    private String mainImage;
    @Field(type = FieldType.Nested)
    private List<String> images;
    private String category;
    private String brand;
    @Field(type = FieldType.Nested)
    private List<String> tags;
    private Integer stock;
    private Integer sales;
    @Field(
        type = FieldType.Date,
        format = DateFormat.epoch_millis // 明确指定以毫秒时间戳形式存储
    )
    private Instant createdAt;

    public ProductES(ProductMySQL productMySQL) {
        this.id = productMySQL.getId();
        this.title = productMySQL.getTitle();
        this.description = productMySQL.getDescription();
        this.price = productMySQL.getPrice();
        this.mainImage = productMySQL.getMainImage();
        this.images = productMySQL.getImages();
        this.category = productMySQL.getCategory();
        this.brand = productMySQL.getBrand();
        this.tags = productMySQL.getTags();
        this.stock = productMySQL.getStock();
        this.sales = productMySQL.getSales();
        this.createdAt = productMySQL.getCreatedAt();
    }
}